package com.tracy.lifeproject.activity;

import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;


import com.google.gson.Gson;
import com.tracy.lifeproject.R;
import com.tracy.lifeproject.adapter.CommodityAdapter;
import com.tracy.lifeproject.bean.CommodityBean;
import com.tracy.lifeproject.constant.UrlString;
import com.tracy.lifeproject.utils.OKHttpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

public class SimpleActivity extends AppCompatActivity {

    @InjectView(R.id.img_simple_back)
    ImageView imgSimpleBack;
    @InjectView(R.id.btn_simple_back)
    Button btnSimpleBack;
    @InjectView(R.id.text_simple_toolbar)
    TextView textSimpleToolbar;
    @InjectView(R.id.img_toolbar_simple)
    ImageView imgToolbarSimple;
    @InjectView(R.id.toolbar_simple)
    Toolbar toolbarSimple;
    @InjectView(R.id.girdView_simple)
    GridView girdViewSimple;

    private List<CommodityBean.DataBean.ItemsBean> mDatas;
    private CommodityAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple);
        ButterKnife.inject(this);

        /**
         * 解析数据
         */
        initData();
        /**
         * 设置适配器
         */
        adapter = new CommodityAdapter(this,mDatas);
        girdViewSimple.setAdapter(adapter);
        /**
         * 接受传过来的值
         */
    }

    /**
     * 解析数据
     *
     */
    private void initData() {
        mDatas = new ArrayList<>();
        OKHttpUtils.newInstance(this).getAsyncMainData(UrlString.URL_SIMPLE, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {
                Toast.makeText(SimpleActivity.this, "下载失败", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onResponse(String result) {
                CommodityBean bean = new Gson().fromJson(result,CommodityBean.class);
                mDatas.addAll(bean.getData().getItems());
                adapter.notifyDataSetChanged();
            }
        },123);
    }

    /**
     * 点击事件
     */
    @OnClick({R.id.img_simple_back, R.id.btn_simple_back, R.id.img_toolbar_simple})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.img_simple_back:
                //finish();
                break;
            case R.id.btn_simple_back:
                //finish();
                break;
            case R.id.img_toolbar_simple:
                break;
        }
    }

}
